#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
/*
 *
linux@linux-vir--mac:~/CProject/IO/day6_bingfa$ ./a.out 
before:
linux@linux-vir--mac:~/CProject/IO/day6_bingfa$ sssss

由于读都关闭了，父进程执行write时父进程异常退出，子进程由init进程收养，执行完逻辑后再退出
 */

int main(){
	int ret;
	int fd[2];
	char buf[5] = {0};

	//创建无名管道
	ret = pipe(fd);
	if(ret < 0){
		perror("pipe");
		return -1;
	}

	pid_t pid = fork();
	if(pid < 0){
		perror("fork");
		return -1;
	}else if(pid > 0){
		close(fd[0]);
		while(1){
			//父进程 写
			strcpy(buf,"abc");
			write(fd[1], buf, strlen(buf));
			sleep(5);
		}
	}else{
		printf("before:\n");
		close(fd[1]);
		close(fd[0]);
		sleep(10);
		printf("sssss\n");
		exit(0);
		while(1){
			//子进程  读
			ret = read(fd[0], buf, 5);
			if(ret > 0){
				printf("str : %s\n", buf);
			}else if(ret == 0){
				printf("ret=0\n");
			}
		}
	}

}
